# encoding: utf-8
"""
MongoQ.py

Created by Edison on 2011-07-20.
Copyright (c) 2011 __MyCompanyName__. All rights reserved.
"""

from pymongo import Connection


class MongoQ(object):
	def __init__(self,host = 'localhost' ,port = 27017):
		self.host = host
		self.port = port
		self.conn = Connection(self.host, self.port)
		# print dir(self.conn)
	# def connection(self):
		
	def close(self):
		self.conn.disconnect()

	def setDB(self, database = '', ):
		self.db = self.conn[database]
		# return self.db
	
	def setCollection(self, collection=''):
		self.collection = self.db[collection]
		
	def getDistinct(self,value):
		return self.collection.distinct(value)

	def getall(self,dict=None):
		return self.collection.find(dict)
		
	def remove(self,dict=None):
		if dict == None:
			return
		else :
			self.collection.remove(dict)
		
if __name__ == '__main__':
	m = MongoQ(host='10.0.1.254')
	# m.connection()
	m.setDB(database='qinzhoukan')
	m.setCollection(collection='itemqueue')
	

	# print m.getDistinct('num_iid')
	for doc in m.getall():
		print doc['num_iid']
		m.remove(dict = {'num_iid':doc['num_iid']})
	
	m.close()
	


